package com.admin.action;

import com.uni.entity.User;
import com.uni.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/29 11:21
 * ------------------------------------------
 */
@WebServlet(name = "AdminInsertUserAction", urlPatterns = "/adminInsertUser")
public class AdminInsertUserAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        User user_check = Service.user.queryUserByName(username);
        // 当用户存在时
        if(user_check != null) {
            resp.getWriter().write("false");
        } else{
            // 添加用户操作
            User user = new User(username, password, phone);
            boolean state = Service.user.registerUser(user);
            if(state){
                // 登陆
                resp.getWriter().write("true");
            } else{
                resp.getWriter().write("false");
            }
        }
    }
}
